package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.VO.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lsx
 * @since 2021-03-01
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

   @Autowired
   private RedisTemplate<String,String> redisTemplate;

    //用户登录
    @Override
    public String login(UcenterMember ucenterMember) {
        //获取前端传递的对象中的手机号和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        //手机号和密码非空校验
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw  new GuliException(20001,"登录失败 手机号或密码为空");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);



        //验证根据手机号获取的对象是否为空
        if(mobileMember==null){ //没有这个手机号
            throw  new GuliException(20001,"登录失败 手机号不存在");
        }
        //判断密码是否正确
        //因为数据库中存得都是加密后的密码 所以在校验时应该将前端传递的铭文进行md5加密  将加密后的密码进行比较
        if(!mobileMember.getPassword().equals(MD5.encrypt(password))){
            throw  new GuliException(20001,"登录失败 密码错误");
        }
        //判断手机号是否被禁用
        if(mobileMember.getIsDisabled()){
            throw  new GuliException(20001,"登录失败 该账号被禁用");
        }
        //验证都通过 登录成功 返回token
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return jwtToken;
    }
    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
        //取出vo中传递的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        //判断数据中是否有空值
        if(StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败 ");
        }

        //判断验证码是否一致
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!redisCode.equals(code)){
            throw new GuliException(20001,"注册失败 验证码错误");
        }

        //查看注册手机号 在数据库中是否已经存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);

        if(count>0){
            //手机号在库中已经存在
            throw new GuliException(20001,"注册失败 该手机号已注册");
        }

        //校验通过  将数据存入数据库中 完成注册
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/de47ee9b-7fec-43c5-8173-13c5f7f689b2.png");//系统默认头像
        ucenterMember.setIsDisabled(false);

        baseMapper.insert(ucenterMember);

    }
    //根据openid查询用户是否是首次注册
    @Override
    public UcenterMember getMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember;
    }
}
